unit EditOrderUnit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ADODB, DBUnit, rxPlacemnt;

type
  TEditOrderForm = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    DateTimePicker1: TDateTimePicker;
    Button1: TButton;
    Button2: TButton;
    FormStorage1: TFormStorage;
  private
    { Private declarations }
  public
    { Public declarations }
    procedure FormSetup(const aOrderId: integer);
  end;

var
  EditOrderForm: TEditOrderForm;

procedure EditOrder(const aOrderId: integer);

implementation

{$R *.dfm}

procedure TEditOrderForm.FormSetup(const aOrderId: integer);
var
  getADOQuery: TADOQuery;
begin
  GetADOQuery:= TADOQuery.Create(Self);
  GetADOQuery.Connection:= DBUnit.DBDataModule.DBADOConnection;
  GetADOQuery.SQL.Clear;
  GetADOQuery.SQL.Add('SELECT Orders.* FROM Orders WHERE Orders.orderid = :aOrderId');
  GetADOQuery.Parameters.ParamByName('aOrderId').Value:= aOrderId;
  GetADOQuery.ExecSQL;
  GetADOQuery.Open;

  Edit1.Text:= GetADOQuery.FieldByName('ordernum').AsString;
  DateTimePicker1.DateTime:= GetADOQuery.FieldByName('orderdate').AsDateTime;

  FreeAndNil(GetADOQuery);
end;

procedure EditOrder(const aOrderId: integer);
var
  updateADOQuery: TADOQuery;
  showResult: integer;
begin
  if (not Assigned(EditOrderForm)) then begin
    EditOrderForm:= TEditOrderForm.Create(nil);
  end;

  EditOrderForm.FormSetup(aOrderId);
  showResult:= EditOrderForm.ShowModal;
  if (showResult = mrOk) then begin
    updateADOQuery:= TADOQuery.Create(nil);
    updateADOQuery.Connection:= DBUnit.DBDataModule.DBADOConnection;
    updateADOQuery.SQL.Clear;
    updateADOQuery.SQL.Add('UPDATE Orders');
    updateADOQuery.SQL.Add('SET ordernum=:aOrderNum,');
    updateADOQuery.SQL.Add('    orderdate=:aOrderDate');
    updateADOQuery.SQL.Add('WHERE Orders.orderid=:aOrderId');
    updateADOQuery.Parameters.ParamByName('aOrderNum').Value:= EditOrderForm.Edit1.Text;
    updateADOQuery.Parameters.ParamByName('aOrderDate').Value:= EditOrderForm.DateTimePicker1.DateTime;
    updateADOQuery.Parameters.ParamByName('aOrderId').Value:= aOrderId;

    updateADOQuery.ExecSQL;

    updateADOQuery.Close;
    updateADOQuery.Active:= False;
    FreeAndNil(updateADOQuery);
  end;
  FreeAndNil(EditOrderForm);
end;

end.
